<?php
/**
 * 限时折扣管理
 *
 * @网店运维提供技术支持 授权请购买shopnc授权
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');
class mb_xianshiControl extends SystemControl{
    public function __construct(){
        parent::__construct();
        Language::read('mobile');
    }

    public function indexOp() {
        $this->xianshi_listOp();
    }
    /**
     * 活动列表
     */
    public function xianshi_listOp(){
        $model_mb_xianshi = Model('mb_xianshi');
        $list = $model_mb_xianshi->getXianshiList(array(), 10);

        Tpl::output('list', $list);
        Tpl::output('page', $model_mb_xianshi->showpage());
        Tpl::setDirquna('mobile');
        Tpl::showpage('mb_xianshi.index');
    }

    /**
     * 输出XML数据
     */
    public function get_xmlOp() {
        $condition = array();

        if (strlen($q = trim($_REQUEST['query']))) {
            switch ($_REQUEST['qtype']) {
                case 'store_name':
                    $condition['store_name'] = array('like', '%'.$q.'%');
                    break;
            }
        }

        $model_xianshi_promotion = Model('p_promotion');
        $list = $model_xianshi_promotion->getXianshiHomeList(array('promotion'=> array('place'=>11),'xianshi'=>$condition ), $_REQUEST['rp'], 'p_xianshi.start_time asc');

        $data = array();
        $data['now_page'] = $model_xianshi_promotion->shownowpage();
        $data['total_num'] = $model_xianshi_promotion->gettotalnum();

        foreach ($list as $val) {
            $o  = '<a class="btn red confirm-on-click" href="javascript:;" data-href="' . urlAdminMobile('mb_xianshi', 'promotion_del', array(
                    'xianshi_id' => $val['xianshi_id'],'xianshi_goods_id'=>$val['xianshi_goods_id']
                )) . '"><i class="fa fa-trash-o"></i>删除</a>';
            if($val['status']==2) {
                $o .= '<a class="btn green confirm-on-click" href="javascript:;" data-href="' . urlAdminMobile('mb_xianshi', 'promotion_approve', array(
                        'xianshi_id' => $val['xianshi_id'], 'xianshi_goods_id' => $val['xianshi_goods_id']
                    )) . '"><i class="fa fa-check"></i>通过</a>';
            }
            $i = array();
            $i['operation'] = $o;

            $i['store_name'] = '<a target="_blank" href="' . urlShop('show_store', 'index', array(
                    'store_id'=>$val['store_id'],
                )) . '">' . $val['store_name'] . '</a>';

            if (isset($flippedOwnShopIds[$val['store_id']])) {
                $i['store_name'] .= '<span class="ownshop">[自营]</span>';
            }
            $i['product_name'] = $val['goods_name'];

            $gi = UPLOAD_SITE_URL.DS.ATTACH_GOODS.DS.$val['goods_image'];
            $i['product_pic'] = <<<EOB
<a href="javascript:;" class="pic-thumb-tip" onMouseOut="toolTip()" onMouseOver="toolTip('<img src=\'{$gi}\'>')">
<i class='fa fa-picture-o'></i></a>
EOB;

            $i['product_price'] = $val['goods_price'];
            $i['product_special'] = $val['xianshi_price'];
            $i['start_time_text'] = date('Y-m-d H:i', $val['start_time']);
            $i['end_time_text'] = date('Y-m-d H:i', $val['end_time']);

            $data['list'][$val['xianshi_goods_id']] = $i;
        }

        echo Tpl::flexigridXML($data);
        exit;
    }

    /**
     * 通过首页每日必抢
     */
    public function promotion_approveOp(){
        $xianshi_goods_id = (int)$_GET['xianshi_goods_id'];
        $xianshi_id = (int)$_GET['xianshi_id'];

        $model_xianshi_promotion = Model('p_promotion');
        $item = $model_xianshi_promotion->getXianshi(array('xianshi_id'=>$xianshi_id,'xianshi_goods_id'=>$xianshi_goods_id));

        $model_xianshi = Model("p_xianshi");
        $xianshi = $model_xianshi->getXianshiInfo(array('xianshi_id'=>$xianshi_id));

        if(empty($item)||$item['status']!=2||empty($xianshi)||$xianshi['state']!=1){
            showMessage(L('param_error'));
        }
        $result = $model_xianshi_promotion->updateXianshiState(array('xianshi_id'=>$xianshi_id,'xianshi_goods_id'=>$xianshi_goods_id),11);
        if($result) {
            $this->log('通过首页限时购物，活动编号'.$item['prom_id']);

            $this->jsonOutput();
        } else {
            $this->jsonOutput('操作失败');
        }
    }

    /**
     * 删除首页每日必抢申请
     */
    public function promotion_delOp(){
        $xianshi_goods_id = (int)$_GET['xianshi_goods_id'];
        $xianshi_id = (int)$_GET['xianshi_id'];

        $model_xianshi_promotion = Model('p_promotion');
        $item = $model_xianshi_promotion->getXianshi(array('xianshi_id'=>$xianshi_id,'xianshi_goods_id'=>$xianshi_goods_id));

        $model_xianshi = Model("p_xianshi");
        $xianshi = $model_xianshi->getXianshiInfo(array('xianshi_id'=>$xianshi_id));

        if(empty($item)||empty($xianshi)||$xianshi['state']!=1){
            showMessage(L('param_error'));
        }
        $result = $model_xianshi_promotion->delXianshiState(array('xianshi_id'=>$xianshi_id,'xianshi_goods_id'=>$xianshi_goods_id));
        if($result) {
            $this->log('删除首页限时购物，活动编号'.$item['prom_id']);

            $this->jsonOutput();
        } else {
            $this->jsonOutput('操作失败');
        }
    }
}
